package oracle.apps.financials.collection.listener;

import greendroid.widget.ItemAdapter;
import greendroid.widget.item.Item;

import java.util.ArrayList;
import java.util.List;

import oracle.apps.financials.collection.R;
import oracle.apps.financials.collection.po.Customer;
import oracle.apps.financials.collection.po.TextLabelItem;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ListView;
import android.widget.PopupWindow;

public class CustomerDetailPopupWindowListener extends BaseOnClickListener {

	private ItemAdapter adapter;

	public CustomerDetailPopupWindowListener(Context context, Customer customer) {
		super(context, R.layout.customer_detail, customer);
	}

	@Override
	public void onClick(View parent) {

		if (popupWindow == null) {
			initPopupwindow();
		}

		popupWindow.showAsDropDown(parent, -70, 2);
	}

	public void initPopupwindow() {
		
		popupWindow = new PopupWindow(this.popupWindowView, 300, 240);

		ListView listView = (ListView) popupWindowView.findViewById(R.id.customer_detail_list);

		adapter = new ItemAdapter(this.context, getDataListItems(customer));

		listView.setAdapter(adapter);

		popupWindow.setTouchInterceptor(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
					popupWindow.dismiss();
					return true;
				}
				return false;
			}

		});
		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
	}

	public List<Item> getDataListItems(Customer customer) {
		List<Item> items = new ArrayList<Item>();
		items.add(new TextLabelItem("Primary Contact: ", customer.getPrimaryContact()));
		items.add(new TextLabelItem("Business Level: ", customer.getBusinessLevel()));
		items.add(new TextLabelItem("Address: ", customer.getAddress()));
		items.add(new TextLabelItem("Email: ", customer.getEmail()));
		items.add(new TextLabelItem("Phone:  ", customer.getPhone()));
		items.add(new TextLabelItem("Strategy: ", customer.getStrategy()));
		return items;
	}

}
